package 数据结构OJ;

public class 链表中倒数第k个节点_04 {
    class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode FindKthToTail(ListNode head, int k) {
        if (head == null)
            return head;
        if (k == 0)
            return null;
        ListNode fast = head;
        ListNode slow = head;
        int i = 0;
        while (++i < k) {
            fast = fast.next;
            if (fast == null)
                return null;
        }

        while (fast.next != null && slow.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        if (slow == null)
            return null;
        else
            return slow;
    }

}
